<?php

namespace IMT\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * IMT\UserBundle\Entity\Participant
 *
 * @ORM\Table(name="contact")
 * @ORM\Entity
 */
class Contact extends Person
{
	/**
	 * @var string $firstname
	 *
	 * @ORM\Column(name="email", type="string")
	 */
	protected $email;
	
	/**
	 * @var string $firstname
	 *
	 * @ORM\Column(name="idGoogle", type="string", nullable=true)
	 */
	protected $idGoogle;
	
    /**
     * @ORM\ManyToOne(targetEntity="IMT\UserBundle\Entity\User", inversedBy="profils")
     * @ORM\joinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;
    
    /**
     * @ORM\ManyToMany(targetEntity="IMT\UserBundle\Entity\GroupContact", inversedBy="contacts")
     * @ORM\JoinTable(name="link_contact_groupcontact",
     *      joinColumns={@ORM\JoinColumn(name="groupcontact_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="contact_id", referencedColumnName="id")}
     * )
     */
    private $groups;
    
    /**
     * @ORM\ManyToMany(targetEntity="IMT\AccountBundle\Entity\Account", mappedBy="contacts")
     */
    private $accounts;
    
    /**
     * @ORM\OneToMany(targetEntity="IMT\Plugins\ParticipantsBundle\Entity\ParticipantEvent", mappedBy="participant", cascade={"all"})
     */
    private $events;
    
    /**
     * @ORM\OneToMany(targetEntity="IMT\UserBundle\Entity\Contact", mappedBy="contactLink", cascade={"detach", "persist", "merge"})
     */
    private $contactUser;
    
    /**
     * @ORM\ManyToOne(targetEntity="IMT\UserBundle\Entity\Contact", inversedBy="contactUser")
     * @ORM\joinColumn(name="parent_id", referencedColumnName="id")
     */
    private $contactLink;
    
    /**
     * Constructor
     */
    public function __construct()
    {
    	$this->user = NULL;
    	$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    	$this->accounts = new \Doctrine\Common\Collections\ArrayCollection();
    	$this->events = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
	 * @return the $idGoogle
	 */
	public function getIdGoogle() {
		return $this->idGoogle;
	}

	/**
	 * @param string $idGoogle
	 */
	public function setIdGoogle($idGoogle) {
		$this->idGoogle = $idGoogle;
	}

	/**
	 * @return the $email
	 */
	public function getEmail() {
		return $this->email;
	}

	/**
	 * @param string $email
	 */
	public function setEmail($email) {
		$this->email = $email;
	}

	public function getPerson(){
    	return parent::$this;
    }
    
	public function getJsonObject($convert = true){
		$jsonObject = parent::getJsonObject(false);
		
    	return $convert === true ? json_encode($jsonObject) : $jsonObject;
    }
    
    public function setPerson(Person $person){
    	$this->setCivility($person->getCivility());
    	$this->setFirstname($person->getFirstname());
    	$this->setName($person->getName());
    }
    
    /**
     * Add user
     *
     * @param IMT\UserBundle\Entity\User $user
     */
    public function setUser(\IMT\UserBundle\Entity\User $user)
    {
        $this->user = $user;
    }

    /**
     * Get user
     *
     * @return IMT\UserBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Add groups
     *
     * @param IMT\UserBundle\Entity\GroupContact $groupcontact
     */
    public function addGroup(\IMT\UserBundle\Entity\GroupContact $groupcontact)
    {
        $this->groups[] = $groupcontact;
    }

    /**
     * Get groups
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getGroups()
    {
        return $this->groups;
    }

    /**
     * Add accounts
     *
     * @param IMT\AccountBundle\Entity\Account $account
     */
    public function addAccount(\IMT\AccountBundle\Entity\Account $account)
    {
        if(!$this->accounts->contains($account)){
        	$this->accounts->add($account);
        }
    }

    /**
     * Get accounts
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getAccounts()
    {
        return $this->accounts;
    }

    /**
     * Add events
     *
     * @param IMT\Plugins\ParticipantsBundle\Entity\ParticipantEvent $event
     */
    public function addParticipantEvent(\IMT\Plugins\ParticipantsBundle\Entity\ParticipantEvent $event)
    {
        $this->events[] = $event;
    }

    /**
     * Get events
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getEvents()
    {
        return $this->events;
    }
    
    public function __toString()
    {
    	return trim($this->firstname.' '.$this->name);
    }
    
	/**
	 * @return the $contactUser
	 */
	public function getContactUser() {		
		return $this->contactUser;
	}

	/**
	 * @return the $contactLink
	 */
	public function getContactLink() {
		return $this->contactLink;
	}

	/**
	 * @param \Doctrine\Common\Collections\ArrayCollection $contactUser
	 */
	public function setContactUser($contactUser) {			
		$this->contactUser = $contactUser;
	}
	
	public function addContactUser($contactUser) {
		if(!$this->contactUser->contains($contactUser) && $contactUser){
			$contactUser->setContactLink($this);
			
			$this->contactUser->add($contactUser);
		}
		
		return $this;
	}

	/**
	 * @param field_type $contactLink
	 */
	public function setContactLink($contactUser) {
		$this->contactLink = $contactUser;
	}
    
}